package com.hua.common.commonview.xlistview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.Point;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import com.hua.common.commontools.DipUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * 横行图表
 */
public class MyHorChat extends LinearLayout {

    private int mWidth = 0;
    private int mHeight = 0;
    /**
     * 黑色坐标线的画笔
     */
    private Paint blackLinePaint = null;

    /**
     * 折线图的画笔
     */
    private Paint brokenLinePaint = null;
    /**
     * 画笔的粗细
     */
    private int blackLinePaintWidth = 0;


    /**
     * 优测文字画笔
     */
    private Paint markPaint = null;

    /**
     * 圆点画笔
     */
    private Paint pointPaint=null;



    /**
     * 刻度字体大小
     */
    private float markTextSize = 0f;

    private double maxSize;


    private int top = 50, left = 150, bottom = 100, right = 100;

    private List<ItemBean> list = null;


    private boolean parped=false;


    public void setList(List<ItemBean> list, double maxSize) {
        this.maxSize = maxSize;
        this.list = list;
    }

    public MyHorChat(Context context) {
        super(context);
        this.init();
    }

    public MyHorChat(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.init();
    }

    public MyHorChat(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.init();
    }


    private void init() {
        this.initMargin();
        this.initPaint();
    }


    private void initMargin() {
        this.top = 30;
        this.left = DipUtil.Dp2px(getContext(), 10);
        this.bottom =30;
        this.right = DipUtil.Dp2px(getContext(), 25);
    }

    /**
     * 初始化画笔
     */
    private void initPaint() {
        this.blackLinePaintWidth = DipUtil.Dp2px(getContext(), 1);
        blackLinePaint = new Paint();
        blackLinePaint.setAntiAlias(true);
        blackLinePaint.setStyle(Paint.Style.STROKE);
        blackLinePaint.setStrokeWidth(this.blackLinePaintWidth);
        blackLinePaint.setDither(true);
        blackLinePaint.setStrokeCap(Paint.Cap.SQUARE); // 当画笔样式为STROKE或FILL_OR_STROKE时，设置笔刷的图形样式，如圆形样式
        // Cap.ROUND,或方形样式Cap.SQUARE
        blackLinePaint.setColor(Color.parseColor("#ff479fc5"));
        PathEffect effects = new DashPathEffect(new float[]{8,8,8,8},8);//设置虚线的间隔和点的长度
        blackLinePaint.setPathEffect(effects);



        this.blackLinePaintWidth = DipUtil.Dp2px(getContext(), 1);
        brokenLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        brokenLinePaint.setAntiAlias(true);
        brokenLinePaint.setStyle(Paint.Style.STROKE);
        brokenLinePaint.setStrokeWidth(this.blackLinePaintWidth);
        brokenLinePaint.setAntiAlias(true); // 消除锯齿
        brokenLinePaint.setDither(true);
        brokenLinePaint.setStrokeCap(Paint.Cap.SQUARE); // 当画笔样式为STROKE或FILL_OR_STROKE时，设置笔刷的图形样式，如圆形样式
        // Cap.ROUND,或方形样式Cap.SQUARE
        brokenLinePaint.setColor(Color.parseColor("#ff479fc5"));


        markTextSize = DipUtil.Sp2px(getContext(), 12f);
        markPaint = new Paint();
        markPaint.setTextSize(markTextSize);
        markPaint.setAntiAlias(true);
        markPaint.setStyle(Paint.Style.FILL);
        markPaint.setStrokeWidth(this.blackLinePaintWidth);
        markPaint.setAntiAlias(true); // 消除锯齿
        markPaint.setDither(true);
        markPaint.setStrokeCap(Paint.Cap.SQUARE); // 当画笔样式为STROKE或FILL_OR_STROKE时，设置笔刷的图形样式，如圆形样式
        // Cap.ROUND,或方形样式Cap.SQUARE
        markPaint.setColor(Color.parseColor("#ffffffff"));




        pointPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        pointPaint.setAntiAlias(true);
        pointPaint.setStyle(Paint.Style.FILL);
        pointPaint.setStrokeWidth(this.blackLinePaintWidth);
        pointPaint.setAntiAlias(true); // 消除锯齿
        pointPaint.setDither(true);
        pointPaint.setStrokeCap(Paint.Cap.SQUARE); // 当画笔样式为STROKE或FILL_OR_STROKE时，设置笔刷的图形样式，如圆形样式
        // Cap.ROUND,或方形样式Cap.SQUARE
        pointPaint.setColor(Color.parseColor("#ff479fc5"));
    }


    /**
     * 获取控件自身的宽度
     */
    private void initWH() {
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        this.initWH();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(parped) {
            drawXY(canvas);
        }
    }


    public void show(){
        this.parped=true;
        this.invalidate();
    }

    /**
     * 绘制坐标系
     *
     * @param canvas
     */
    private void drawXY(Canvas canvas) {
        int itemHeight = (mHeight-top-bottom) / 8;
        List<Point> pointlist=new ArrayList<>();

        for (int i = 0; i < 8; i++) {
            float startX=left;
            float startY= itemHeight * i+top;
            float endX= (float) (mWidth / 3*2 * list.get(i).getItemSize() / maxSize)+left;
            float endY=startY;
            Path path=new Path();
            path.moveTo(startX,startY);
            path.lineTo(endX,endY);
            canvas.drawPath(path,blackLinePaint);
            canvas.drawText(list.get(i).getShowText(), mWidth /3*2 + left+40, endY, markPaint);

            canvas.drawCircle(endX, endY, 10, pointPaint);    // 大圆
            Point point=new Point((int)endX,(int) endY);
            pointlist.add(point);
        }
        Path path1 = new Path();//梯形
        for(int i=0;i<pointlist.size();i++){
            if(i==0){
                path1.moveTo(pointlist.get(i).x, pointlist.get(i).y);//绘画基点
            }else{
                path1.lineTo(pointlist.get(i).x, pointlist.get(i).y);
            }
        }
        canvas.drawPath(path1, brokenLinePaint);
    }



    public static class ItemBean {
        public ItemBean() {
        }

        private String showText = null;

        public String getShowText() {
            return showText;
        }

        public void setShowText(String showText) {
            this.showText = showText;
        }

        private double itemSize;

        public double getItemSize() {
            return itemSize;
        }

        public void setItemSize(double itemSize) {
            this.itemSize = itemSize;
        }
    }
}
